package cn.skyquant.quant4j.service.boot.web.controller;

import cn.skyquant.quant4j.api.dto.ResultDTO;
import cn.skyquant.quant4j.api.ea.EAManagerDTO;
import cn.skyquant.quant4j.api.ea.EAService;
import cn.skyquant.quant4j.sdk.util.bean.BeanTools;
import cn.skyquant.quant4j.service.boot.model.account.Account;
import cn.skyquant.quant4j.service.boot.model.account.AccountRepository;
import cn.skyquant.quant4j.service.boot.model.authority.RoleAuthority;
import cn.skyquant.quant4j.service.boot.model.ea.EaManager;
import cn.skyquant.quant4j.service.boot.model.ea.EaManagerRepository;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/ea")
@RoleAuthority(roleNames = {RoleAuthority.administrator,RoleAuthority.client})
public class EAController {

    @Autowired
    EAService eaService;
    @Autowired
    EaManagerRepository eaManagerRepository;
    @Autowired
    AccountRepository accountRepository;

    @RequestMapping(value = "/list",method = RequestMethod.GET)
    @ResponseBody
    public ResultDTO<List<EAManagerDTO>> get(
            @RequestParam(value = "ea",required = false) String ea){
        Iterable<EaManager> iterable = null;
        if(ea==null || "all".equals(ea)){
            iterable = eaManagerRepository.findAll();
        }else {
            iterable = eaManagerRepository.findByEa(ea);
        }
        List<EAManagerDTO> list = Lists.newArrayList();
        iterable.forEach(x->list.add(convertDoToDto(x)));
        return ResultDTO.success(list);
    }

//    @RequestMapping(value = "",method = RequestMethod.GET)
//    @ResponseBody
//    public ResultDTO<EAManagerDTO> get(
//            @RequestParam(value = "ea",required = true) String ea,
//            @RequestParam(value = "eaId",required = true) Integer eaId,
//            @RequestParam(value = "accountCode",required = true) String accountCode,
//            @RequestParam(value = "borkerType",required = true) BorkerType borkerType){
//        return eaService.getByKey(ea,eaId,accountCode,borkerType);
//    }

    @RequestMapping(value = "",method = RequestMethod.POST)
    @ResponseBody
    public ResultDTO<EAManagerDTO> getOrCreate(
            @RequestParam(value = "ea",required = true) String ea,
            @RequestParam(value = "eaId",required = true) Integer eaId,
            @RequestParam(value = "accountId",required = true)Integer accountId){
        return eaService.getOrCreate(ea,eaId,accountId);
    }

    //准备启动EA
    @RequestMapping(value = "/{id}/ready",method = RequestMethod.POST)
    @ResponseBody
    public ResultDTO<Void> ready(
            @PathVariable(value = "id",required = true) Integer id,
            @RequestParam(value = "hostname",required = true) String hostname,
            @RequestParam(value = "localIp",required = true) String localIp,
            HttpServletRequest request){
        String remoteIp = request.getHeader("X-Real-IP");
        return eaService.ready(id,hostname,localIp,remoteIp);
    }

    //启动EA
    @RequestMapping(value = "/{id}/start",method = RequestMethod.POST)
    @ResponseBody
    public ResultDTO<Void> start(
            @PathVariable(value = "id",required = true) Integer id){
        return eaService.start(id);
    }

    //启动EA
    @RequestMapping(value = "/{id}/stop",method = RequestMethod.POST)
    @ResponseBody
    public ResultDTO<Void> stop(
            @PathVariable(value = "id",required = true) Integer id){
        return eaService.stop(id);
    }

    //发送心跳
    @RequestMapping(value = "/{id}/heartbeat",method = RequestMethod.POST)
    @ResponseBody
    ResultDTO<Void> heartbeat(
            @PathVariable(value = "id",required = true) Integer id,
            @RequestParam(value = "borkerTime",required = true) String borkerTime,
            HttpServletRequest request){
        String remoteIp = request.getHeader("X-Real-IP");
        return eaService.heartbeat(id,borkerTime,remoteIp);
    }

    private EAManagerDTO convertDoToDto(EaManager entity) {
        EAManagerDTO dto = new EAManagerDTO();
        BeanTools.copyPublicFields(entity,dto);
        Optional<Account> optionalAccount = accountRepository.findById(entity.id);
        if(optionalAccount.isPresent()){
            dto.username = optionalAccount.get().user.name;
        }
        return dto;
    }

}
